<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/1/11
 * Time: 8:47
 */
class LoginModel extends Model{
      // 定义表名
      protected $tableName = 'admin_login';

      /**
       * @param $data 查询数据
       * @return int 返回值
       */
      public function checkLogin($data){
            $where['account'] = $data['username'];
            $where['password'] = $data['pwd'];
            $res = $this->where($where)->find();
            if($res){
                 return $res;
            }else{
                 return 0;
            }
      }

      /**
       * @param $condition 条件
       * @param $data 更新数据
       * @return int 返回值
       */
      public function updatauser($condition,$data){
           $res = $this->where($condition)->save($data);
           if($res){
                  return 1;
           }else{
                  return 0;
           }
      }

    /**
     * 登录账户安全检测
     * @param $res 登录信息
     */
      public function security_check($res){
            $count = 10;
            $salfe = 9; // 账号安全状态
            $nowip = get_client_ip(); // 本次登录ip
            // 如果登录==上次登录ip 常用登录次数加一 否则建一 为
            if($nowip==$res['last_login_ip']){
                   if($res['offen_login_ip_time']<10)
                         $res['offen_login_ip_time']+=1;
            }else{
                   if($res['offen_login_ip_time']>0) {
                         $res['offen_login_ip_time']-=1;
                   }
            }

            //账号安全状态
            switch($res['offen_login_ip_time']){
                  case 8:
                        $salfe = 8; // 提醒
                        break;
                  case 5:
                        $salfe = 5; // 警告
                        break;
                  case 3:
                        $salfe = 3; // 危险
                        break;
                  case 1:
                        $salfe = 1; // 严重
            }

            // 设置常用登录ip
            if($res['offen_login_ip_time']>$count){
                   $res['offen_login_ip'] = $nowip;
            }

            $res['login_ip'] = $nowip;
            $res['last_login_ip'] = $res['login_ip'];
            $res['account_salfe_status'] = $salfe;
            $res['last_login_time'] = $res['login_now_time'];
            $res['login_times'] = $res['login_times']+1;
            $res['login_now_time'] = time();
            $this->where("id=$res[id]")->save($res);
      }

}